package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.ImageListByTypeResponse;

/**
 * Created by Alan on 2018/4/4.
 */

public class ImageCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Context mContext;
    private LayoutInflater inflater;
    private OnClickItemListener mOnChangeItem;

    public ImageCollectionAdapter(Context context, List<ViewBean> datas, OnClickItemListener mOnChangeItem) {
        this.mContext = context;
        this.mDatas = datas;
        this.mOnChangeItem = mOnChangeItem;
        inflater = LayoutInflater.from(mContext);
    }

    public void clearData() {
        mDatas.clear();
    }

    public enum ViewType {
        TYPE_BASE,
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public static class TitleBean {
            public String time;

            public TitleBean(String time) {
                this.time = time;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_BASE.ordinal()) {
            holder = new BaseHolder(inflater.inflate(R.layout.item_image_collection_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof BaseHolder) {
            ImageListByTypeResponse.PicListBean temp = (ImageListByTypeResponse.PicListBean) viewBean.getObj();
            BaseHolder baseHolder = (BaseHolder) holder;

            baseHolder.itemImageCollectionIv.setImageURI(temp.getSmallPicPath());
            baseHolder.itemImageCollectionIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnChangeItem.onClick(position);
                }
            });
        }
    }

    class BaseHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView itemImageCollectionIv;

        public BaseHolder(View view) {
            super(view);
            itemImageCollectionIv = view.findViewById(R.id.itemImageCollectionIv);
        }
    }

    public interface OnClickItemListener {
        void onClick(int position);
    }
}
